#include <cstdio> 
#include <algorithm>
using namespace std;
template <typename T>
inline void read(T& x)
{
    int c=getchar(), f=1; x=0;
    while(c<'0'||'9'<c) {if(c=='-') f=-1; c=getchar();}
    while('0'<=c&&c<='9') 
        x=(x<<3)+(x<<1)+c-'0', c=getchar();
    x*=f;
}

inline void write(long long x)
{
    if(x>=10) write(x/10);
    putchar(x%10+'0');
}
#define DEBUG
using ll=long long;
const int N=1e4+10, M=1e6+10;
int n, m;
int a[N];
ll dp[M];
void init()
{
    read(n), read(m);
    for(int i=1; i<=m; i++) read(a[i]);
    memset(dp, -1, sizeof dp);
    dp[0]=0;
}

void solve()
{
    init();
    for(int i=0; i<=n; i++) //枚举当前重量
        for(int j=m; j; j--) //第j个物品
            for(int k=0; k*j<=i; k++) //用k个
                dp[i]=max(dp[i], dp[i-j*k]+k*a[j]);
    write(dp[n]); puts("");
}

signed main()
{
    #ifdef DEBUG
        freopen("../in.txt", "r", stdin);
        freopen("../out.txt", "w", stdout);
    #endif

    int T=1; //scanf("%d", &T);
    while(T--) 
    {
        solve();
    }
    return 0;
}